<template>
  <div class="login-setting">
    <el-form :model="userInfo" label-width="100px" :rules="userForm">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="userInfo.username" placeholder="" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="userInfo.password" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="hSubmit"> 确定 </el-button>
        <el-button type="info" @click="hSubmit">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getEmployeeDetailAPI, updateEmployeeDetailAPI } from '@/api/employees'
export default {
  props: {
    curId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      // 表单数据
      userInfo: {
        username: '',
        password: ''
      },
      // 表单校验
      userForm: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getEmployeeDetail()
  },
  methods: {
    // 表单提交按钮
    hSubmit() {
      this.updateEmployeeDetail()
    },
    // 保存信息
    async updateEmployeeDetail() {
      try {
        await updateEmployeeDetailAPI(this.userInfo)
        this.$message.success('修改成功')
        // 保存数据
      } catch (err) {
        console.log('', err)
      }
    },
    // 数据回填
    async getEmployeeDetail() {
      try {
        const res = await getEmployeeDetailAPI(this.curId)
        console.log('', res)
        // 保存数据
        this.userInfo = res.data
      } catch (err) {
        console.log('', err)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.login-setting {
  padding-right: 600px;
}
</style>
>
